import {
    queryCustomers,
    createCustomer,
    updateCustomerBase,
    getCustomerById,
    deleteCustomerById,
    entqualification,
    getUnsettleorder,
    getCstAll,
    getHistoryAmtList,
    getStatetemntDetail,
    getApprovaList,
    createCustomerExtra,
    updateCustomerExtra,
    createFactotyCustomer,
    updateWebsiteCompany,
    updateMpCustomerBase
} from '../../services/customer/customer';
import {
    addAgencyagreement,
    updateAgencyagreement,
    getAgencyagreementById,
    deleteAgencyagreementById,
    getAgencyagreementList
} from '../../services/customer/agencyagreement';
import {
    getLinkmanList,
    addLinkman,
    updateLinkman,
    getLinkmanById,
    deleteLinkmanById
} from '../../services/customer/linkman';
import {
    getRecAccList,
    addRecAcc,
    updateRecAcc,
    getRecAccById,
    deleteRecAccById,
    getRecAccListOfCstid,
    queryCstRecAccs
} from '../../services/customer/RecAcc';
import {getBuyerList, addBuyer, updateBuyer, getBuyerById, deleteBuyerById} from '../../services/customer/Buyer';
import {addMarket, deleteMarket, getMarketById, getListByCstIdForPage} from '../../services/customer/marketInfo';
import {addStatement, createMpStatement} from '../../services/capital/approval';
import {getManagerList} from '../../services/auth/user';
import OperatorConstant from "../../utils/OperatorConstant";
import Constants from '../../utils/Constants';
import {message} from "antd";

const R = require('ramda');

export default {
    namespace: 'customerManage',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        customerPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        linkmanPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        agencyagreementPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        recAccPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        }, buyerPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        customerBankPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        marketInfoPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        customerSatus: "4",

        list: [],
        historyAmtList: [],
        unsettleorderList: [],
        buyerList: [],
        linkmanList: [],
        agencyagreementList: [],
        recAccList: [],
        approvaList: [],
        marketInfoList: [],
        WebsiteCompanyList: [],

        websiteCompanyData: {},
        statetemntDetail: {},
        attachments: {},
        dataSource: {},
        manager: {},
        linkman: {},
        agencyagreement: {},
        recAcc: {},
        customer: {},
        buyer: {},
        marketInfo: {},
        extra: {},  //客户交接单
        factoryUser: {}, //开设工厂客户
        //银企直联列表
        customerBankList: [],
        subAccountList: [],//客户所有的银企直联子账号列表
        recAccsList: [], //收款人下拉列表
        cstList: []     //客户下拉列表
    },
    reducers: {

        //-----------------setList

        /**
         * 设置数据列表
         * @param state
         * @param payload
         * @returns {{list, page}}
         */
        setCustomerManageList(state, {payload}) {
            return {...state, list: payload.list, page: payload.page, customerSatus: payload.status}
        },

        /**
         * 设置联系人数据列表
         * @param state
         * @param payload
         * @returns {{linkmanList: (Array|*), linkmanPage: *}}
         */
        setLinkmanList(state, {payload}) {
            return {...state, linkmanList: payload.linkmanList, linkmanPage: payload.linkmanPage}
        },


        /**
         * 设置协议数据列表
         * @param state
         * @param payload
         * @returns {{agencyagreementList: (Array|*), agencyagreementPage: *}}
         */
        setAgencyagreementList(state, {payload}) {
            return {
                ...state,
                agencyagreementList: payload.agencyagreementList,
                agencyagreementPage: payload.agencyagreementPage
            }
        },

        /**
         * 设置付款人列表
         * @param state
         * @param payload
         * @returns {{recAccList: (Array|*), recAccPage: (state.recAccPage|{page, size, totalRow, totalPage}|*)}}
         */
        setRecAccList(state, {payload}) {
            return {...state, recAccList: payload.recAccList, recAccPage: payload.recAccPage}
        },


        /**
         * 设置境外贸易商列表
         * @param state
         * @param payload
         * @returns {{buyerList: *, buyerPage: (state.buyerPage|{page, size, totalRow, totalPage}|*)}}
         */
        setBuyerList(state, {payload}) {
            return {...state, buyerList: payload.buyerList, buyerPage: payload.buyerPage}
        },


        /**
         * 设置未来结算订单
         * @param state
         * @param payload
         * @returns {{unsettleorderList}}
         */
        setUnsettleorder(state, {payload}) {
            return {...state, unsettleorderList: payload.list}
        },


        /**
         * 设置历史余额
         * @param state
         * @param payload
         * @returns {{unsettleorderList}}
         */
        setHistoryAmtList(state, {payload}) {
            return {...state, historyAmtList: payload.list}
        },


        /**
         * 设置审核记录
         * @param state
         * @param action
         * @returns {{approvaList}}
         */
        setApprovaList(state, {payload}) {
            return {...state, approvaList: payload.list}
        },

        //--------------------setEntity

        /**
         * 设置协议数据
         * @param state
         * @param action
         * @returns {{agencyagreement: string}}
         */
        setAgencyagreement(state, action) {
            let payload = action.payload;
            return {...state, agencyagreement: payload.agencyagreement,}
        },


        /**
         * 设置联系人
         * @param state
         * @param action
         * @returns {{linkman: *}}
         */
        setlinkman(state, action) {
            let payload = action.payload;
            return {...state, linkman: payload.linkman,}
        },

        /**
         * 设置管理人员
         * @param state
         * @param action
         * @returns {{dataSource: (state.customer|{}|*|payload.customer|string), attachments: (state.attachments|{}|*)}}
         */
        setManager(state, action) {
            let payload = action.payload;
            return {...state, manager: payload.manager};
        },

        /**
         * 设置收款人账户信息
         * @param state
         * @param action
         * @returns {{recAcc: (state.recAcc|{}|*)}}
         */
        setRecAcc(state, action) {
            let payload = action.payload;
            return {...state, recAcc: payload.recAcc,};
        },

        /**
         * 设置境外贸易商
         * @param state
         * @param action
         * @returns {{buyer: (payload.buyer|{})}}
         */
        setBuyer(state, action) {
            let payload = action.payload;
            return {...state, buyer: payload.buyer,};
        },

        /**
         * 设置结算详情
         * @param state
         * @param action
         * @returns {{statetemntDetail}}
         */
        setStatetemntDetail(state, action) {
            let payload = action.payload;
            return {...state, statetemntDetail: payload.data,};
        },

        /**
         * 设置银企直联列表
         * @param state
         * @param payload
         * @returns {{unsettleorderList}}
         */
        setCustomerBankList(state, {payload}) {
            return {...state, customerBankList: payload.list}
        },

        /**
         * 设置营销线索信息
         * @param state
         * @param action
         * @returns {{marketInfo: (state.marketInfo|{}|*)}}
         */
        setMarketInfo(state, action) {
            let payload = action.payload;
            return {...state, marketInfo: payload.marketInfo,};
        },

        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, {payload}) {
            return {...state, ...payload};
        }
    },
    effects: {
        /**
         * 分页查询客户
         *
         * @param call
         * @param put
         * @param select
         */* queryCustomerManageList({payload}, {call, put, select}) {

            let page = yield select(state => state.customerManage.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            let customerSatus = yield select(state => state.customerManage.customerSatus);
            customerSatus = R.isNil(payload) || R.isNil(payload.status) ? customerSatus : payload.status;

            const res = yield call(queryCustomers, {
                customerNO: R.isNil(payload) || R.isNil(payload.customerNO) ? "" : payload.customerNO,
                cstName: R.isNil(payload) || R.isNil(payload.cstName) ? "" : payload.cstName,
                cstType: R.isNil(payload) || R.isNil(payload.cstType) ? "" : payload.cstType,
                cstManage: R.isNil(payload) || R.isNil(payload.cstManage) ? "" : payload.cstManage,
                cstWaiter: R.isNil(payload) || R.isNil(payload.cstWaiter) ? "" : payload.cstWaiter,
                beginTime: R.isNil(payload) || R.isNil(payload.beginTime) ? "" : payload.beginTime,
                endTime: R.isNil(payload) || R.isNil(payload.endTime) ? "" : payload.endTime,
                businessType: R.isNil(payload) || R.isNil(payload.businessType) ? "" : payload.businessType,
                status: customerSatus,
                page: page
            });


            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                let listWithKey = R.isNil(content.list) ? [] : content.list;
                R.map((order) => {
                    order.key = order.id;
                }, listWithKey)
                yield put({
                    type: 'setCustomerManageList',
                    payload: {
                        list: listWithKey,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status: customerSatus,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取未结算订单
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getUnsettleorder({payload}, {call, put, select}) {
            const res = yield call(getUnsettleorder, {cstId: payload.cstId});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setUnsettleorder",
                    payload: {
                        list: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },


        /**
         * 获取历史余额列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getHistoryAmtList({payload}, {call, put, select}) {
            const res = yield call(getHistoryAmtList, {cstId: payload.cstId});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setHistoryAmtList",
                    payload: {
                        list: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 获取审核记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getApprovaList({payload}, {call, put, select}) {
            const res = yield call(getApprovaList, {cstId: payload.cstId});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setApprovaList",
                    payload: {
                        list: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },


        /**
         * 获取结算详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getStatetemntDetail({payload}, {call, put, select}) {
            const res = yield call(getStatetemntDetail, {
                type: payload.type,
                cstId: payload.cstId,
                orderIds: payload.orderIds
            });

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setStatetemntDetail",
                    payload: {
                        data: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 根据id客户详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getCustomerById({payload}, {call, put, select}) {
            const res = yield call(getCustomerById, {
                id: payload.id,
                isApproval: R.isNil(payload.isApproval) ? false : payload.isApproval
            });

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                let customer = data.customer;
                let attachments = data.attachments;
                let approvals = data.approvals;
                //模拟创建的代码 start
                let emptyOrder = {id: customer.id};
                //模拟创建代码 end
                yield put({
                    type: "refreshState",
                    payload: {
                        dataSource: customer, //模拟创建就用空的表单 emptyOrder
                        attachments: attachments,
                        approvaList: approvals,
                        extra: data.extra,
                        subAccountList: data.subAccountList,
                        factoryUser: data.factoryUser,
                    }
                });
                if (payload.onSuccess) payload.onSuccess(customer);
            } else {
                message.error(res.data.errMessage);
            }

        },


        /**
         * 创建结算单
         * @param payload
         * @param call
         * @param put
         * @param select
         */* addStatement({payload}, {call, put, select}) {
            const res = yield call(addStatement, payload);
            let status = yield select(state => state.customerManage.customerSatus);

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                message.info("创建结算单成功！");

                if (payload.onSuccess) payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 创建结算单-市场采购
         * @param payload
         * @param call
         * @param put
         * @param select
         */* createMpStatement({payload}, {call, put, select}) {
            const res = yield call(createMpStatement, payload);
            let status = yield select(state => state.customerManage.customerSatus);

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                message.info("创建结算单成功！");

                if (payload.onSuccess) payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 创建客户
         *
         * @param payload
         * @param call
         * @param select
         * @param put
         */* createCustomer({payload}, {call, select, put}) {
            const res = yield call(createCustomer, payload);
            let status = yield select(state => state.customerManage.customerSatus);

            if (res.data.resultCode === "ok") {
                message.info("客户创建成功！");

                if (payload.onSuccess()) payload.onSuccess();


            } else {
                message.error(res.data.errMessage);

            }
        },

        /**
         *  删除客户
         * @param payload
         * @param call
         * @param put
         * @param select
         */* deleteCustomerById({payload}, {call, put, select}) {
            let status = yield select(state => state.customerManage.customerSatus);
            const res = yield call(deleteCustomerById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("客户删除成功！");

                if (payload.onSuccess()) payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 更新客户信息
         * @param payload
         * @param call
         * @param select
         * @param put
         */* updateCustomerBase({payload}, {call, select, put}) {
            let status = yield select(state => state.customerManage.customerSatus);
            const res = yield call(updateCustomerBase, payload);

            if (res.data.resultCode === "ok") {
                message.info(status === 2 ? "客户提交审核成功！" : "客户保存成功！");
                if (payload.onSuccess) payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 修改协议信息
         * @param payload
         * @param call
         * @param select
         * @param put
         */* entqualification({payload}, {call, select, put}) {
            let status = yield select(state => state.customerManage.customerSatus);
            const res = yield call(entqualification, payload);

            if (res.data.resultCode === "ok") {
                message.info("修改协议信息成功！");

                if (payload.onSuccess()) payload.onSuccess();


            } else {
                message.error(res.data.errMessage);

            }
        },


        /**
         * 获取管理人员
         * @param payload
         * @param call
         * @param select
         * @param put
         */* getManagerList({payload}, {call, select, put}) {
            const res = yield call(getManagerList, {});
            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setManager",
                    payload: {
                        manager: data,
                    }
                });
            }
        },

        //--------------客户代理协议

        /**
         * 增加客户代理协议表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* addAgencyagreement({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.agencyagreementPage);
            const res = yield call(addAgencyagreement, {...R.omit(['onSuccess', 'OnFail'])(payload)});

            if (res.data.resultCode === "ok") {
                message.info("增加客户代理协议成功");
                if (payload.onSuccess) payload.onSuccess();

                yield put({
                    type: 'getAgencyagreementList',
                    payload: {
                        id: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 更新客户代理协议表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* updateAgencyagreement({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.agencyagreementPage);
            const res = yield call(updateAgencyagreement, {...R.omit(['onSuccess', 'OnFail'])(payload)});

            if (res.data.resultCode === "ok") {
                message.info("修改客户代理协议成功");
                if (payload.onSuccess) payload.onSuccess();

                yield put({
                    type: 'getAgencyagreementList',
                    payload: {
                        id: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取客户代理协议表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getAgencyagreementById({payload}, {call, put, select}) {
            const res = yield call(getAgencyagreementById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setAgencyagreement",
                    payload: {
                        agencyagreement: data,
                    }
                });
            }


        },

        /**
         * 删除客户代理协议表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* deleteAgencyagreementById({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.agencyagreementPage);
            const res = yield call(deleteAgencyagreementById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("删除客户代理协议成功");

                yield put({
                    type: 'getAgencyagreementList',
                    payload: {
                        id: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取客户代理协议表列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getAgencyagreementList({payload}, {call, put, select}) {

            let page = payload.page;
            const res = yield call(getAgencyagreementList, {id: payload.id, page});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setAgencyagreementList',
                    payload: {
                        agencyagreementList: R.isNil(content.list) ? [] : content.list,
                        agencyagreementPage: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        //--------------联系人

        /**
         * 获取联系人列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getLinkmanList({payload}, {call, put, select}) {

            let page = payload.page;
            const res = yield call(getLinkmanList, {cstId: payload.cstId, status: "1", page});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setLinkmanList',
                    payload: {
                        linkmanList: R.isNil(content.list) ? [] : content.list,
                        linkmanPage: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加联系人
         * @param payload
         * @param call
         * @param put
         * @param select
         */* addLinkman({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.linkmanPage);
            const res = yield call(addLinkman,
                {
                    name: payload.linkman.name,
                    sex: payload.linkman.sex,
                    position: payload.linkman.position,
                    telephone: payload.linkman.telephone,
                    mobile: payload.linkman.mobile,
                    email: payload.linkman.email,
                    cstid: payload.linkman.cstid,
                });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("增加联系人成功");
                if (payload.onSuccess) payload.onSuccess();
                yield put({
                    type: 'getLinkmanList',
                    payload: {
                        cstId: payload.linkman.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新联系人
         * @param payload
         * @param call
         * @param put
         * @param select
         */* updateLinkman({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.linkmanPage);
            const res = yield call(updateLinkman,
                {
                    id: payload.linkman.id,
                    name: payload.linkman.name,
                    sex: payload.linkman.sex,
                    position: payload.linkman.position,
                    telephone: payload.linkman.telephone,
                    mobile: payload.linkman.mobile,
                    email: payload.linkman.email,
                    cstid: payload.linkman.cstid,
                });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("修改联系人成功");
                if (payload.onSuccess) payload.onSuccess();
                yield put({
                    type: 'getLinkmanList',
                    payload: {
                        cstId: payload.linkman.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 删除联系人
         * @param payload
         * @param call
         * @param put
         * @param select
         */* deleteLinkmanById({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.linkmanPage);
            const res = yield call(deleteLinkmanById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("删除联系人成功");

                yield put({
                    type: 'getLinkmanList',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取联系人
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getLinkmanById({payload}, {call, put, select}) {
            const res = yield call(getLinkmanById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setlinkman",
                    payload: {
                        linkman: data,
                    }
                });
            }
        },


        //--------------收款人


        /**
         * 获取收款人账户列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getRecAccList({payload}, {call, put, select}) {
            let page = payload.page;
            const res = yield call(getRecAccList, {cstId: payload.cstId, status: "1", page});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setRecAccList',
                    payload: {
                        recAccList: R.isNil(content.list) ? [] : content.list,
                        recAccPage: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取收款人账户下拉
         * @param cstId
         * @param call
         * @param put
         * @param select
         */* getRecAccListOfCstid({cstId}, {call, put, select}) {
            const res = yield call(getRecAccListOfCstid, {cstId});
            if (res.data.resultCode === 'ok') {
                let data = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        recAccsList: data
                    }
                });
            }
        },


        /**
         * 获取收款人账户详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getRecAccById({payload}, {call, put, select}) {
            const res = yield call(getRecAccById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setRecAcc",
                    payload: {
                        recAcc: data,
                    }
                });
            }
        },

        /**
         * 删除收款人账户
         * @param payload
         * @param call
         * @param put
         * @param select
         */* deleteRecAccById({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.recAccPage);
            const res = yield call(deleteRecAccById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("删除收款人账户成功");

                yield put({
                    type: 'getRecAccList',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加收款人账户
         * @param payload
         * @param call
         * @param put
         * @param select
         */* addRecAcc({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.recAccPage);
            const res = yield call(addRecAcc, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("增加收款人账户成功");
                if (payload.onSuccess) payload.onSuccess();
                yield put({
                    type: 'getRecAccList',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新收款人账户
         * @param payload
         * @param call
         * @param put
         * @param select
         */* updateRecAcc({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.recAccPage);
            const res = yield call(updateRecAcc, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("修改收款人账户成功");
                if (payload.onSuccess) payload.onSuccess();
                yield put({
                    type: 'getRecAccList',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


        //------------------------境外贸易商

        /**
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getBuyerList({payload}, {call, put, select}) {
            let page = payload.page;
            const res = yield call(getBuyerList, {cstId: payload.cstId, status: "1", page});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setBuyerList',
                    payload: {
                        buyerList: R.isNil(content.list) ? [] : content.list,
                        buyerPage: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getBuyerById({payload}, {call, put, select}) {
            const res = yield call(getBuyerById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setBuyer",
                    payload: {
                        buyer: data,
                    }
                });
            }
        },

        /**
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */* deleteBuyerById({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.buyerPage);
            const res = yield call(deleteBuyerById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("删除境外贸易商成功");

                yield put({
                    type: 'getBuyerList',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */* addBuyer({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.buyerPage);
            //处理外商账号
            payload.accounts.map((account) => account.id && R.contains("new", account.id) ? account.id = null : account);
            const res = yield call(addBuyer, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("增加境外贸易商成功");
                if (payload.onSuccess) payload.onSuccess();
                yield put({
                    type: 'getBuyerList',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */* updateBuyer({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.buyerPage);

            //处理外商账号
            payload.accounts.map((account) => account.id && R.contains("new", account.id) ? account.id = null : account);
            const res = yield call(updateBuyer, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("修改境外贸易商成功");
                if (payload.onSuccess) payload.onSuccess();
                yield put({
                    type: 'getBuyerList',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


        //----------------------------营销线索


        /**
         * 分页获取客户营销线索
         * @param payload
         * @param call
         * @param put
         */* getListByCstIdForPage({payload}, {call, put}) {
            const res = yield call(getListByCstIdForPage, {...R.omit(['onSuccess', 'onFail'])(payload)});

            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        marketInfoList: res.data.content.list,
                        marketInfoPage: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 根据id获取营销线索详情
         *
         * @param id
         */* getMarketById({payload}, {call, put}) {
            if (payload.id) {
                const res = yield call(getMarketById, {...R.omit(['onSuccess', 'onFail'])(payload)});
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            marketInfo: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        marketInfo: {}
                    }
                });
            }
        },

        /**
         * 新增营销线索
         *
         * @param call
         */* addMarket({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.marketInfoPage);
            const res = yield call(addMarket, payload);
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("营销线索新增成功！");
                if (payload.onSuccess) payload.onSuccess();
                yield put({
                    type: 'getListByCstIdForPage',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 删除营销线索
         *
         * @param id
         */* deleteMarket({payload}, {call, put, select}) {
            let page = yield select(state => state.customerManage.marketInfoPage);
            const res = yield call(deleteMarket, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("营销线索删除成功！");

                yield put({
                    type: 'getListByCstIdForPage',
                    payload: {
                        cstId: payload.cstid,
                        page: page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 保存客户交接单
         *
         * @param call
         */* createCustomerExtra({payload}, {call}) {
            const res = yield call(createCustomerExtra, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("客户交接单保存成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 更新客户交接单
         *
         * @param call
         */* updateCustomerExtra({payload}, {call}) {
            const res = yield call(updateCustomerExtra, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("客户交接单更新成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 开设工厂客户账号
         *
         * @param call
         */* createFactotyCustomer({payload}, {call}) {
            const res = yield call(createFactotyCustomer, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("开设工厂用户账号成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 官网认证企业
         */* getWebsiteCompanyList({payload}, {call, put}) {
            const res = yield call(queryCustomers, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        WebsiteCompanyList: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 根据id查询官网企业详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */* getWebsiteById({payload}, {call, put, select}) {
            const res = yield call(getCustomerById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                let customer = data.customer;
                let attachments = data.attachments;

                yield put({
                    type: "refreshState",
                    payload: {
                        websiteCompanyData: customer,
                        attachments: attachments,
                    }
                });
                if (payload.onSuccess) payload.onSuccess(customer);
            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 修改官网认证企业审核状态
         *
         * @param call
         */* updateWebsiteCompany({payload}, {call}) {
            const res = yield call(updateWebsiteCompany, payload);
            if (res.data.resultCode === "ok") {
                message.info("审核官网认证企业成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 市场采购
         * 修改客户信息--基本信息
         *
         * @param call
         */* updateMpCustomerBase({payload}, {call}) {
            const res = yield call(updateMpCustomerBase, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res.data.resultCode === "ok") {
                message.info("客户提交成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 查询当前租户所有的客户
         *
         * @param call
         * @param put
         * @param select
         */* getCstAll({}, {call, put, select}) {
            const res = yield call(getCstAll, {});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        cstList: R.isNil(content) ? [] : content,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取当前客户有效的付款账号
         *
         * @param call
         * @param put
         * @param select
         */* queryCstRecAccs({payload}, {call, put, select}) {
            const res = yield call(queryCstRecAccs, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        recAccsList: R.isNil(content) ? [] : content.list,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
    },
    subscriptions: {
        setup({history, dispatch, bb}) {

        }
    }
};
